import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import { Modal, Form, message } from 'antd';
import moment from 'moment';
import TeacherCalendarSearchInput from './TeacherCalendarSearchInput';
import { myMessage } from '../../utils/formatDataSource';
const FormItem = Form.Item;
@connect(({ administration3, loading }) => ({
  administration:administration3,
  loading: loading.models.administration3,
}))
@Form.create()
class TeacherCalendarChangeSupplyTeacher extends PureComponent {
  constructor(props) {
    super(props);
  }
  state = {
    addVisible: this.props.supplyVisible,
    classList: [],
    selectedTeacher: {},
  };
  componentWillUpdate() {
    this.setState({
      visible: this.props.supplyVisible,
      selectedEvents: this.props.selectedEvents,
      events: this.props.events,
      onCancel: this.props.onCancel,
      onOk: this.props.onOk,
    });
  }

  handleChangeCancel = () => {
    this.props.onCancel();
  };
  handleChangeUpdate = () => {
    const { dispatch } = this.props;
    const { selectedEvents, events, selectedTeacher } = this.state;
    selectedEvents.teacherId = selectedTeacher.id;
    selectedEvents.supplyTeacherName = selectedTeacher.name;
    // 获取组装数据  服务器请求完成

    dispatch({
      type: 'administration3/updateLessonTeacher',
      payload: 'lessonId=' + selectedEvents.id + '&teacherId=' + selectedTeacher.id,
    }).then(() => {
      const { updateLessonTeacherResponse } = this.props.administration;
      if (updateLessonTeacherResponse.data == 1) {
        myMessage.success('修改成功', 2);
        const idx = events.indexOf(selectedEvents);
        const nextEvents = [...events];
        nextEvents.splice(idx, 1, selectedEvents);

        this.props.onOk(nextEvents);
        this.setState({ addVisible: false });
      }
    });
  };
  handleSelectSupplyTeacher = teacher => {
    this.setState({
      selectedTeacher: teacher,
    });
  };
  render() {
    const { supplyVisible, selectedEvents } = this.props;
    return (
      <div>
        <Modal
          visible={supplyVisible}
          title="代课"
          okText="确定"
          onCancel={this.handleChangeCancel}
          onOk={this.handleChangeUpdate}
        >
          <div>
            请选择代课老师{' '}
            <TeacherCalendarSearchInput
              myStyle={150}
              onOk={this.handleSelectSupplyTeacher}
              subject={selectedEvents.subject}
            />
          </div>
        </Modal>
      </div>
    );
  }
}

export default TeacherCalendarChangeSupplyTeacher;
